package com.gucheng.aigc.manager;

import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ImageManager {

    private final ImageSynthesis imageSynthesis = new ImageSynthesis();

    public String generateImage(String prompt) {
        ImageSynthesisParam param = ImageSynthesisParam.builder()
                .model(ImageSynthesis.Models.WANX_V1)
                .n(1)
                .size("1024*1024")
                .apiKey("sk-003f6b51d6b14d0f9e320713c12a8b94")
                .prompt(prompt)
                .build();

        String result = "";
        try {
            ImageSynthesisResult synthesisResult = imageSynthesis.call(param);
            result = synthesisResult.toString();
            log.info("图片生成成功 : {}", result);
        } catch (ApiException | NoApiKeyException e) {
            log.error("图片生成失败 : " + e.getMessage());
        }
        return result;
    }
}
